﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace WebSite
{
    public partial class MainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            string userName = (string)(Session["UserName"]);
            string isUserLoggedIn = (string)(Session["isUserLoggedIn"]);
            UserBalance_Label.Text = "Balance: " + getuserBalance(userName) + " RON";

            if (string.IsNullOrEmpty(isUserLoggedIn))
            {
                isUserLoggedIn = "false";
                Response.Redirect("Login.aspx");
            }
            else if (isUserLoggedIn.Equals("false"))
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                isUserLoggedIn = "true";
                User_Label.Text = "Logged in as: " + userName;
            }
        }

        private int getuserBalance(string UserName)
        {
            WebService.SOAPServiceClient webservice = new WebService.SOAPServiceClient();
            WebService.Users user = webservice.readUser(UserName);
            int balance = user.Balance;
            return balance;
        }

        protected void Logout_Button_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Contents.RemoveAll();
            FormsAuthentication.SignOut();
            Session["isUserLoggedIn"] = "false";
            Response.Redirect("Login.aspx");
        }

        protected void ViewPastOrders_Button_Click(object sender, EventArgs e)
        {
            TopUp_Panel.Visible = false;
            GridView_Panel.Visible = true;
            BookingOrders_Label.Visible = true;
           
            WebService.SOAPServiceClient webservice = new WebService.SOAPServiceClient();
            WebService.UsersBooking[] usersBookingsbyUserLoggedin = webservice.getallUsersBookingsbyUsername((string)(Session["UserName"]));

            int[] allbookingNumbers = new int[usersBookingsbyUserLoggedin.Length];
            WebService.Booking[] neededBookingsList = new WebService.Booking[allbookingNumbers.Length];


            //populate allbookingNumbers with all booking Numbers
            for (int i = 0; i < usersBookingsbyUserLoggedin.Length; i++)
            {
                allbookingNumbers[i] = usersBookingsbyUserLoggedin[i].BookingNO;
            }


            for (int i = 0; i < allbookingNumbers.Length; i++)
            {
                neededBookingsList = webservice.getallBookingsbybookingNO(allbookingNumbers[i]);
            }

            List<WebService.Booking> neededBookingslst = neededBookingsList.ToList();

            if (neededBookingslst.Count == 0)
            {                
                BookingOrders_Label.Text = "No bookings found.";
            }
            GridView1.DataSource = neededBookingslst;
            GridView1.DataBind();
        }

        protected void TopUp_Button_Click(object sender, EventArgs e)
        {
            GridView_Panel.Visible = false;
            TopUp_Panel.Visible = true;
            BookingOrders_Label.Visible = false;
        }

        protected void Pay_button_Click(object sender, EventArgs e)
        {
            string balanceTEMP = BalanceValues_DropDownList.SelectedValue.ToString();
            string balanceString = balanceTEMP.Replace("RON", "");
            int TOPUP_Balance = int.Parse(balanceString);
            int currentBalance = getuserBalance((string)(Session["UserName"]));
            int toADD_Balance = currentBalance + TOPUP_Balance;
            
            WebService.SOAPServiceClient webservice = new WebService.SOAPServiceClient();
            webservice.updateUserbalance((string)(Session["UserName"]), toADD_Balance);

            UserBalance_Label.Text = "Balance: " + getuserBalance((string)(Session["UserName"])) + " RON";
        }
    }
}